package com.wzy.flyweight;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: wzy
 * @Date: 2021/12/05/13:54
 * @Description: 享元模式
 */
public class Client {
    public static void main(String[] args) {
       /* FlyweightFactory flyweightFactory = FlyweightFactory.getFlyweightFactory();
        flyweightFactory.getFlyweight("爱").operation("位置1");
        flyweightFactory.getFlyweight("我").operation("位置2");
        flyweightFactory.getFlyweight("的").operation("位置3");
        flyweightFactory.getFlyweight("人").operation("位置4");
        flyweightFactory.getFlyweight("和").operation("位置5");
        flyweightFactory.getFlyweight("我").operation("位置6");
        flyweightFactory.getFlyweight("爱").operation("位置7");
        flyweightFactory.getFlyweight("的").operation("位置8");
        flyweightFactory.getFlyweight("人").operation("位置9");
        System.out.println("已存储的享元对象个数："+flyweightFactory.getFlyweightMap().size()+"个");
        for (Entry<String, Flyweight> entry : flyweightFactory.getFlyweightMap().entrySet()) {
            System.out.println(entry.getValue());
        }*/
        List<String> intrinsicStates = new ArrayList<String>();
        intrinsicStates.add("木须肉");
        intrinsicStates.add("地三鲜");
        intrinsicStates.add("孜然牛肉");

        FlyweightFactory flyFactory =FlyweightFactory.getFlyweightFactory();

        UnsharedConcreteFlyweight unsharedFlyweight2 = flyFactory.getUnsharedFlyweight(intrinsicStates);
        intrinsicStates.add("霸王餐");
        UnsharedConcreteFlyweight unsharedFlyweight1 = flyFactory.getUnsharedFlyweight(intrinsicStates);

        System.out.println("复合享元对象是否可以共享：" + (unsharedFlyweight1 == unsharedFlyweight2));

        unsharedFlyweight1.operation("一号桌");
        unsharedFlyweight2.operation("五号桌");

        //开始给一号桌上菜了
        unsharedFlyweight1.getFlyweight("木须肉");
        unsharedFlyweight1.getFlyweight("地三鲜");
        unsharedFlyweight1.getFlyweight("孜然牛肉");
        unsharedFlyweight1.getFlyweight("霸王餐");

        // 二号桌
        unsharedFlyweight2.getFlyweight("霸王餐");

        System.out.println();

        String intrinsicState ="木须肉";
        Flyweight flyweight1 = unsharedFlyweight1.getFlyweightMap().get(intrinsicState);
        Flyweight flyweight2 = unsharedFlyweight2.getFlyweightMap().get(intrinsicState);
        System.out.println("不同复合享元对象中的单纯享元对象是否可以共享：" + (flyweight1 == flyweight2));
    }
}
